The base cloner API is quite simple. A cloner factory is acquired, a configuration is created, and the cloner is constructed from the factory and configuration. It can then be used to create object clones.
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.jboss.marshalling.cloner.ClonerConfiguration;
import org.jboss.marshalling.cloner.ObjectCloner;
import org.jboss.marshalling.cloner.ObjectClonerFactory;
import org.jboss.marshalling.cloner.ObjectCloners;
public final class CloningExample {
public static void main(String[] args) {
final ObjectClonerFactory clonerFactory = ObjectCloners.getSerializingObjectClonerFactory();
final ClonerConfiguration configuration = new ClonerConfiguration();
final ObjectCloner cloner = clonerFactory.createCloner(configuration);
try {
final List<String> clone = (List<String>) cloner.clone(Arrays.asList("One", "two", "three"));
for (String s : clone) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Note that IOException is among the possible exceptions thrown. This is because the cloner uses the same object construction logic that the marshalling layer uses.
There are a number of advanced features available to the user of the cloning API. See the section Cloning for more information.